package com.gtjy.webserver4j;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;


public class Server {

	private InetAddress address;
	private int port;
	private ServerSocket server;
	
	public Server() {
		this(1000);
	}

	public Server(int port) {
		this.port = port;
	}

	public void start(){

		try {
			address = InetAddress.getLocalHost();
			server = new ServerSocket();
			server.bind(new InetSocketAddress(address, port));
			System.out.println("Starting Web Server...");
			while(true){
				Socket client = server.accept();
				new ConnectionHandler(client);
			}
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public int getPort() {
		return port;
	}

	public InetAddress getAddress() {
		return address;
	}
}
